package com.ks.app.navigate.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.Query;
import org.springframework.stereotype.Service;
import com.ks.app.admin.hibernate.KsOperaterLog;
import com.ks.app.admin.service.KsoperatelogService;
import com.ks.app.admin.service.impl.BaseServiceImpl;
import com.ks.app.admin.util.Pager;
import com.ks.app.navigate.hibernate.KsWebsite;
import com.ks.app.navigate.service.KsWebsiteService;

@Service("ksWebsiteService")
public class KsWebsiteServiceImpl extends BaseServiceImpl implements KsWebsiteService{

	@Resource(name="ksoperatelogService")
	private KsoperatelogService logService;
	
	/**
	 * 通过主键获取
	 */
	@Override
	public KsWebsite findKsWebsiteById(long id) {
		return em.find(KsWebsite.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	/**
	 * 站点分页列表
	 * name    站点名称
	 * navigateid  导航条
	 * status      上线状态
	 * 
	 */
	public List<KsWebsite> findKsWebsiteList(String name,long navigateid, int status,int recommend,
			Pager pager) {
		
		
		String sql  = " from KsWebsite where 1=1 ";
		String csql = " select count(*) from KsWebsite where 1=1";
		
		if (navigateid != -1) {
			sql  = sql + "  and navigateid="+navigateid;
			csql = csql + "  and navigateid="+navigateid;
		}
		
		if(status != 0){
			sql  = sql +" and status="+status;
			csql = csql +" and status="+status;
		}
		
		if(recommend != 0){
			sql  = sql +" and recommend="+recommend;
			csql = csql +" and recommend="+recommend;
		}
		
		if(!"".equals(name)){
			sql  = sql + "  and instr(name,?)>0";
			csql = csql + "  and instr(name,?)>0";
		}
		
		sql  = sql +" order by ordernum desc";
		
		Query query = em.createQuery(sql);
		Query cquery = em.createQuery(csql);
		
		if(!"".equals(name)){
			query.setParameter(1, name);
			cquery.setParameter(1, name);
		}
		
	
		query.setMaxResults(pager.getCntOfPage());
		query.setFirstResult((pager.getCurPage() - 1) * pager.getCntOfPage());
		pager.setTotoalCnt(Integer.parseInt(String.valueOf(cquery.getSingleResult())));
		List<KsWebsite> list = query.getResultList();
		
		return list;
	}
	
	
	/**
	 * 新增和修改
	 */
	@Override
	public void saveKsWebsite(KsWebsite website) {
		em.merge(website);
	}

	@SuppressWarnings("unchecked")
	@Override
	/**
	 * 获取某导航条下的所有上线站点
	 */
	public List<KsWebsite> findKsWebsiteList(long navigateid) {
		
		String hql = "from KsWebsite where status=2 and navigateid="+navigateid;
		List<KsWebsite> list = em.createQuery(hql).getResultList();
		
		return list;
	}

	/**
	 * 通过主键删除站点
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void deleKsWebsites(String webids,String usrname) {
		
		String hql = "from KsWebsite where id in("+webids+")";
		List<KsWebsite> list = em.createQuery(hql).getResultList();
		
		String content = "";
		for(KsWebsite w : list){
			content += w.getName()+",";
			em.remove(w);
		}
		
		KsOperaterLog log = new KsOperaterLog();
		log.setContent(content);
		log.setLogtime(new Date());
		log.setLogtype(1);
		log.setOpename("删除站点");
		log.setOperater(usrname);
		log.setStatus(1);
		
		logService.save(log);
		
		//String hql = "delete KsWebsite where id in("+webids+")";
		//em.createQuery(hql).executeUpdate();
		
	}

	/**
	 * 获取所有上线站点
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<KsWebsite> findOnlineWeblist() {
		
		String hql = " from KsWebsite where status =2 order by ordernum desc";
		
		return em.createQuery(hql).getResultList();
	}

	/**
	 * 获取推荐站点
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<KsWebsite> findRecommendWebList() {
		
		String hql = " from KsWebsite where status =2 and recommend=2 order by ordernum";
		return em.createQuery(hql).getResultList();
	}

}
